﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FastCtr
{
    public partial class ProductTypeForm : Form
    {
        static ProductTypeForm productTypeform = new ProductTypeForm();
        DirectoryInfo di = FastData.SystemPath.strDirPath;
        string mpath = "";
        string _contempType = "000000";
        event Action<string> onProductTypeChanged;



        public static string ProductTypeName
        {
            get
            {
                return productTypeform._contempType;
            }
        }

        public static Action<string> OnProductTypeChanged
        {
            get
            {
                return productTypeform.onProductTypeChanged;
            }
            set
            {
                productTypeform.onProductTypeChanged = value;
            }
        }



        private ProductTypeForm()
        {
            InitializeComponent();
            try
            {
                _contempType = (string)FastData.SaveStatic.ReadBinF("lastRunProductTypeName");
            }
            catch { }
        }

        public static ProductTypeForm GetThis(string path)
        {
            productTypeform.mpath = path;
            return productTypeform;
        }



        private void ProductType_ParentChanged(object sender, EventArgs e)
        {
            if (ParentForm != null)
            {
                //ParentForm.FormClosing += new FormClosingEventHandler(ParentForm_FormClosing);
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }
        private void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ParentForm.Controls.Clear();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            DirectoryInfo directory = new DirectoryInfo(mpath);
            DirectoryInfo[] dics = directory.GetDirectories();
            for (int i = 0; i < dics.Length; i++)
            {
                if (!doubleBufferListView1.Items.ContainsKey(dics[i].Name))
                {
                    doubleBufferListView1.Items.Add(dics[i].Name, (doubleBufferListView1.Items.Count + 1).ToString(), 0);
                    doubleBufferListView1.Items[dics[i].Name].SubItems.Add(dics[i].Name);
                }
            }
            textBox1.Text = _contempType;
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            doubleBufferListView1.Items.Clear();
            DirectoryInfo directory = new DirectoryInfo(mpath);
            DirectoryInfo[] dics = directory.GetDirectories();
            for (int i = 0; i < dics.Length; i++)
            {
                if (!doubleBufferListView1.Items.ContainsKey(dics[i].Name))
                {
                    doubleBufferListView1.Items.Add(dics[i].Name, (doubleBufferListView1.Items.Count + 1).ToString(), 0);
                    doubleBufferListView1.Items[i].SubItems.Add(dics[i].Name);
                }
            }
        }

        private void doubleBufferListView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (doubleBufferListView1.SelectedItems.Count == 1)
            {
                _contempType = doubleBufferListView1.SelectedItems[0].SubItems[1].Text;
                onProductTypeChanged?.Invoke(_contempType);
                FastData.SaveStatic.SaveBinF("lastRunProductTypeName", _contempType);
                MessageBox.Show("型号切换成功！\r\n已经切换到型号:" + _contempType, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("请选择一个型号！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            InputBox inputbox = new InputBox();
            if (!string.IsNullOrEmpty(inputbox.DialogValue))
            {
                if (!Directory.Exists(mpath + "\\" + inputbox.DialogValue))
                {
                    Directory.CreateDirectory(mpath + "\\" + inputbox.DialogValue + "\\模板");
                    Directory.CreateDirectory(mpath + "\\" + inputbox.DialogValue + "\\LineGuage");
                    Directory.CreateDirectory(mpath + "\\" + inputbox.DialogValue + "\\CircleGuage");
                    Directory.CreateDirectory(mpath + "\\" + inputbox.DialogValue + "\\Camera");
                    MessageBox.Show("型号"+ inputbox.DialogValue + "创建成功！");
                }
                else
                {
                    MessageBox.Show("型号已存在！");
                }
            }
        }
    }
}
